﻿using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace Silverlight.Controls
{
    /// <summary>
    /// Represents a control that creates a pop-up window that displays information for an element in the UI.
    /// </summary>
    public class ToolTip : System.Windows.Controls.ToolTip
    {

        /// <summary>
        /// Identifies the ToolTip.DisplayTime dependency property.
        /// </summary>
        /// <remarks>Default value is 5 seconds.</remarks>
        public static readonly DependencyProperty DisplayTimeProperty
            = DependencyProperty.RegisterAttached("DisplayTime", typeof (Duration), typeof (ToolTip),
                                                  new PropertyMetadata(new Duration(TimeSpan.FromSeconds(5)), OnDisplayTimePropertyChanged));

        /// <summary>
        /// Identifies the ToolTip.InitialDelay dependency property.
        /// </summary>
        /// <remarks>Default value is 1 second.</remarks>
        public static readonly DependencyProperty InitialDelayProperty
            = DependencyProperty.RegisterAttached("InitialDelay", typeof (Duration), typeof (ToolTip),
                                                  new PropertyMetadata(new Duration(TimeSpan.FromSeconds(1)), OnInitialDelayPropertyChanged));
        /// <summary>
        /// Identifies the ToolTip.CloseAnimation dependency property.
        /// </summary>
        /// <remarks>Default value is null.</remarks>
        public static readonly DependencyProperty CloseAnimationProperty
            = DependencyProperty.RegisterAttached("CloseAnimation", typeof(Storyboard), typeof(ToolTip),
                                                  new PropertyMetadata(null, OnCloseAnimationPropertyChanged));

        /// <summary>
        /// Identifies the ToolTip.OpenAnimation dependency property.
        /// </summary>
        /// <remarks>Default value is null.</remarks>
        public static readonly DependencyProperty OpenAnimationProperty
            = DependencyProperty.RegisterAttached("OpenAnimation", typeof(Storyboard), typeof(ToolTip),
                                                  new PropertyMetadata(null, OnOpenAnimationPropertyChanged));

        private static void OnCloseAnimationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is ToolTip))
            {
                throw new InvalidOperationException("You can only set ToolTip.CloseAnimationProperty on ToolTip object.");
            }
        }
        private static void OnOpenAnimationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is ToolTip))
            {
                throw new InvalidOperationException("You can only set ToolTip.OpenAnimationProperty on ToolTip object.");
            }
        }
        private static void OnInitialDelayPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is ToolTip))
            {
                throw new InvalidOperationException("You can only set ToolTip.DisplayTimeProperty on ToolTip object.");
            }
        }
        private static void OnDisplayTimePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is ToolTip))
            {
                throw new InvalidOperationException("You can only set ToolTip.DisplayTimeProperty on ToolTip object.");
            }
        }

        /// <summary>
        /// Gets or sets the display time of this ToolTip instance in seconds.
        /// </summary>
        /// <remarks>
        /// The default value is 5 seconds.
        /// </remarks>
        public Duration DisplayTime
        {
            get { return (Duration)GetValue(DisplayTimeProperty); }
            set { SetValue(DisplayTimeProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Storyboard to execute when closing the ToolTip.
        /// </summary>
        public Storyboard CloseAnimation
        {
            get { return (Storyboard)GetValue(CloseAnimationProperty); }
            set { SetValue(CloseAnimationProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Storyboard to execute when opening the ToolTip.
        /// </summary>
        public Storyboard OpenAnimation
        {
            get { return (Storyboard)GetValue(OpenAnimationProperty); }
            set { SetValue(OpenAnimationProperty, value); }
        }

        /// <summary>
        /// Gets or sets the initial delay for the tooltip to show in seconds.
        /// </summary>
        /// <remarks>
        /// The default value is 1 second.
        /// </remarks>
        public Duration InitialDelay
        {
            get { return (Duration)GetValue(InitialDelayProperty); }
            set { SetValue(InitialDelayProperty, value); }
        }

    }
}
